package com.atm.bulksms;



import java.util.Vector;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.PhoneLookup;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;

import com.atm.bulksms.data.ContactItem;
import com.atm.bulksms.data.BulkSMSData.GroupData;
import com.atm.bulksms.dialog.GroupContactListDialog;
import com.atm.bulksms.util.Constants;
import com.atm.bulksms.util.Utils;


public class GroupListActivity extends ListActivity implements OnItemClickListener{

	private static final int DIALOG_GROUP_CONTACTS = 0;
	public static final int DIALOG_GROUP_REMOVED = 1;
	public static final int DIALOG_GROUP_SAVED = 2;
	protected static final int DIALOG_GROUP_ADD = 3;
    SimpleCursorAdapter mAdapter;

    int listType;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       Cursor cursor =  getContentResolver().query(GroupData.CONTENT_URI, null,GroupData.GROUP_NAME+ " NOT NULL) GROUP BY ("+GroupData.GROUP_NAME, null, null);
        mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, new String[]{GroupData.GROUP_NAME}, new int[]{android.R.id.text1});
        setListAdapter(mAdapter);
        getListView().setOnItemClickListener(this);
        
        Bundle bundle = getIntent().getExtras();
        if(bundle != null){
        	listType = bundle.getInt(Constants.LIST_TYPE);
        }
    }
    String selectedGroupName;
    Cursor selectedCursor;

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
		Cursor groupCursor = (Cursor)getListAdapter().getItem(position);
		selectedGroupName = groupCursor.getString(groupCursor.getColumnIndex(GroupData.GROUP_NAME));
		System.out.println("GroupName "+selectedGroupName);
		selectedCursor = getContentResolver().query(GroupData.CONTENT_URI, null,GroupData.GROUP_NAME+"=\""+selectedGroupName+"\"", null, null);
		if(listType == Constants.LIST_TYPE_GROUP_EDIT){
			showDialog(DIALOG_GROUP_CONTACTS);
		}else{
			Intent intent = new Intent();
			int contactId = -1;
			while(selectedCursor.moveToNext()){
				//ContactItem item = new ContactItem();
				String no = selectedCursor.getString(selectedCursor.getColumnIndex(GroupData.PHONE_NUMBER));
				Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(no));
				Cursor c =  getContentResolver().query(uri, new String[]{PhoneLookup._ID, PhoneLookup.DISPLAY_NAME}, null, null, null);
				String name = "";
				if(c != null && c.getCount() > 0){
					c.moveToFirst();
					name = c.getString(c.getColumnIndex(PhoneLookup.DISPLAY_NAME));
					contactId = c.getInt(c.getColumnIndex(PhoneLookup._ID));
				}
				System.out.println(" name : "+name+" contactId "+contactId);
				ContactItem item = new ContactItem(contactId, name, no);
				c.close();
				//Utils.addContact(contactId, item);
				Utils.addContactInGroup(selectedGroupName, item);
				System.out.println(" Added ");
				Utils.removeContact(contactId);
			}
			setResult(RESULT_OK, intent);
			finish();
		}
	}

   @Override
protected void onPrepareDialog(int id, Dialog dialog) {
	// TODO Auto-generated method stub
	   switch (id) {
	case DIALOG_GROUP_CONTACTS:
		((GroupContactListDialog)dialog).updateGroup(this, selectedGroupName, selectedCursor);
		System.out.println("updated");
		break;
	case DIALOG_GROUP_ADD:
		((GroupContactListDialog)dialog).updateGroup();
		break;
	default:
		break;
	}
	super.onPrepareDialog(id, dialog);
}

   GroupContactListDialog groupDialog;
	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;
		switch (id) {
		case DIALOG_GROUP_CONTACTS:
			 dialog = new GroupContactListDialog(this, GroupContactListDialog.TYPE_EDIT_GROUP);
			 System.out.println(" created");
		
			 break;
		case DIALOG_GROUP_SAVED:
			dialog = Utils.getDialog(this, "Alert", "Group Saved with recent changes.");
			break;
		case DIALOG_GROUP_REMOVED:
			dialog = Utils.getDialog(this, "Alert", "Group removed from the list.");
			break;
		case DIALOG_GROUP_ADD:
			 dialog = new GroupContactListDialog(this, GroupContactListDialog.TYPE_CREATE_GROUP);
			 break;
		
		default:
			dialog = super.onCreateDialog(id);
			break;
		}
		return dialog;
	}

	@Override
	protected Dialog onCreateDialog(int id, Bundle args) {
		// TODO Auto-generated method stub
		Dialog dialog ;
		switch (id) {
		case Constants.DIALOG_MESSAGE:
			dialog = Utils.getDialog(this, args.getString(Constants.TITLE), args.getString(Constants.MESSAGE));
			break;

		default:
			dialog = super.onCreateDialog(id, args);
			break;
		}
		return dialog;
	}
	@Override
	protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
		// TODO Auto-generated method stub
		if(id == Constants.DIALOG_MESSAGE){
			dialog.setTitle(args.getString(Constants.TITLE));
			((AlertDialog)dialog).setMessage(args.getString(Constants.MESSAGE));
		}
		super.onPrepareDialog(id, dialog, args);
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
	
	Utils.setCursor(selectedCursor);
	}
	
	@Override
	protected void onRestoreInstanceState(Bundle state) {
		// TODO Auto-generated method stub
		super.onRestoreInstanceState(state);
		selectedCursor = Utils.getCursor();
		selectedCursor.requery();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add(0, 0, 0, "Add Group");
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId() == 0){
			showDialog(DIALOG_GROUP_ADD);
		}
		return true;
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode == RESULT_OK && data != null){
			showDialog(DIALOG_GROUP_ADD);
		}
	}
}

